#include "data_global.h"
#include "sqlite_link_list.h"
extern pthread_mutex_t mutex_slinklist;
extern pthread_cond_t cond_analysis;
extern slinklist slinkHead;
extern int sqlite_InsertLinknode(slinklist,struct env_info_clien_addr,int ,int);
extern int sqlite_EmptyLinklist(slinklist);
extern pthread_cond_t cond_sqlite;
extern struct sqlite_operation sqlite_operation_t;
pthread_cond_t cond_transfer;
extern struct env_info_clien_addr all_info_RT;
extern struct env_info_clien_addr env_info_clien_addr_s;
extern int storageNum;
extern int goodsKinds;
extern pthread_cond_t cond_goods;
extern pthread_mutex_t mutex_goods;
extern pthread_cond_t cond_lock;
extern pthread_mutex_t mutex_trans;
struct setEnv1
{
	char snum;
	char temp[2];
	char hum[2];
	char x;
	char y;
	char z;
	int ill;
	int bet;
	int adc;
};
struct tam
{
	int year;
	char mon;
	char day;
	char hour;
	char min;
	char src;
};
void serial_init(int fd)
{
	struct termios options;
	tcgetattr(fd,&options);  //获取属性(termios 结构)
	options.c_cflag |= (CLOCAL | CREAD); //忽略控制线，设置接收字符
	options.c_cflag &= ~CSIZE;             //传输字符的位数
	options.c_cflag &= ~CRTSCTS;           //使用硬件流控制
	options.c_cflag &= ~CSTOPB; 			//设置一位的停止位
	options.c_cflag |= CS8; 				//每个字符8位
	options.c_iflag |= IGNPAR; 				//忽略寄偶校验错误
	options.c_oflag = 0; 					//输出模式
	options.c_lflag = 0; 					//本地模式

	if(cfsetispeed(&options,B115200) == -1)    //设置输入波特率9600
	{
		printf("cfsetispeed is error\n");
		exit(1);
	}
	tcsetattr(fd,TCSANOW,&options);   		//激活新配置
	printf("init ok\n");
}

void *pthread_transfer(void*msg)
{ 

	int dev_uart_fd,n,a;
	time_t t;
	char buf[20]={0};
	char buf2[4]={0};
	struct env_info_clien_addr T;
	struct setEnv1 env1;
	struct tm *tm;
	char buf_time[20] = {0};

#if 1
	dev_uart_fd=open(DEV_ZIGBEE,O_RDWR);
	if(dev_uart_fd==-1)
	{
		perror("open fd failed");
	}
	serial_init(dev_uart_fd);
	sleep(1);
#endif
	while(1)
	{
		bzero(buf2,sizeof(buf2));
		bzero(buf,sizeof(buf));
		int count = 0;
		while((n=read(dev_uart_fd,buf2,4))>0)
		{
			count ++;
			printf("sec = %d, min = %d\n", count , count/60);
			t=time((time_t *)NULL);
			tm=localtime(&t);
			sprintf(buf_time, "%d/%d/%d %d:%d:%d",tm->tm_year+1900,
												  tm->tm_mon+1,
												  tm->tm_mday,
								     			  tm->tm_hour,
									  			  tm->tm_min,
												  tm->tm_sec);
			printf("time = %s\n", buf_time);

			if(strncmp(buf2,"st:",3)==0)
			{
				if(buf2[3]=='e')
				{
					//	sqlite_operation_t.table_select_mask=1;  //环境还是货物e/r
					if(read(dev_uart_fd,buf,20)<0)
					{
						perror("read 20 err");
					}
					memcpy(&env1,buf,20);
					sleep(1);


					a = buf[0];
					T.storage_no[a].storage_status = 1;
					T.storage_no[a].temperature=(float)env1.temp[0];
					T.storage_no[a].humidity=(float)env1.hum[0];
					T.storage_no[a].illumination=(float)env1.ill;
					T.storage_no[a].x=env1.x;
					T.storage_no[a].y=env1.y;
					T.storage_no[a].z=env1.z;
					T.storage_no[a].battery=(float)env1.bet/10;
					T.storage_no[a].adc=(float)env1.adc/1024*3.3;
					strcpy(T.storage_no[a].samplingTime, buf_time);

					//usleep(500000);
					pthread_mutex_lock(&mutex_slinklist);
					sqlite_InsertLinknode(slinkHead, T, a, 0);
					sqlite_operation_t.table_select_mask = ENV_OP_MASK;
					sqlite_operation_t.env_operation_mask = COLLECT_INSERTER;
					pthread_cond_signal(&cond_sqlite);
					pthread_mutex_unlock(&mutex_slinklist);

					puts("transfer++++++");
				}
#if 1
				else if(buf2[3]=='r')
				{
					if((read(dev_uart_fd,buf,4))<0)
					{
						perror("read 8 err");
					}

					a = buf[0];
					printf("goods_type:%c\n",buf[1]);
					printf("++++++++++++++++++++++++++++++++++++++goods_count:%d\n",buf[3]);
					T.storage_no[a].goods_info[buf[2]].goods_type=(unsigned char)buf[2];
					T.storage_no[a].goods_info[buf[2]].goods_count=(unsigned int)buf[3];
					strcpy(T.storage_no[a].samplingTime, buf_time);
					//goods_no=buf[2];

					env_info_clien_addr_s = T;
					storageNum = a;
					goodsKinds = (int)(buf[2]);
					if(buf[1] == 'I')
					{
						sqlite_operation_t.goods_operation_mask = GOODS_ADD;
						sqlite_operation_t.table_select_mask = GOO_OP_MASK;
					}
					if(buf[1] == 'O')
					{
						sqlite_operation_t.goods_operation_mask = GOODS_REDUCE;
						sqlite_operation_t.table_select_mask = GOO_OP_MASK;
					}

					pthread_cond_signal(&cond_sqlite);
					pthread_mutex_lock(&mutex_goods);
					pthread_cond_wait(&cond_goods, &mutex_goods);
					pthread_mutex_unlock(&mutex_goods);
				}
#if 0
				pthread_mutex_lock(&mutex_trans);
				puts("cond_lock wait 1");
				pthread_cond_wait(&cond_lock, &mutex_trans);
				puts("cond_lock wait 2");
				pthread_mutex_unlock(&mutex_trans);
#endif
#endif
			}
		}
	}
	close(dev_uart_fd);
	exit(0);
}
